      function onRangeChange(){
    	var pos = new google.maps.LatLng(loc.lat, loc.lng);
	    deleteCircles();
	    var radius = getRadius();
	    map.setZoom(getMapZoomLevel(radius));
	    drawCircle(radius);
    } 
      function getMapZoomLevel(radius)
      {
    	var radiusLevel = parseInt(radius/10);
  	    if (radiusLevel <= 2)
  	    	{
  	    		zoomLevel = 11 - radiusLevel;
  	    	}
  	    else if (radiusLevel <= 5)
  	    	{
  	    		zoomLevel = 8;
  	    	}
  	    else if (radiusLevel <= 10)
  	    	{
  	    		zoomLevel = 7;
  	    	}
  	    else 
  	    	{
  	    		zoomLevel = 7 - parseInt(radius/100);
  	    	}
    	  return zoomLevel;
      }
  	$(document).ready(function() {
		$('input#txt_range').change(function() {
			onRangeChange();
		});
		// configure the range slider.
		if(document.getElementById('rangeSlider') != null)
			{
				$("#rangeSlider").slider({
					range: "min",
					value: $("#rangeValue").text(),
					min: 1,
					max: 250,
					slide: function(event,ui) {
						$("#txt_range").val(ui.value).trigger("change");
						$("#rangeValue").text(ui.value);
					}
				});
			}
}); 


      var map;
      var markers = [];
      var circles = [];
      var loc = {};
      var isEditable;
      
      function initialize() {
    	  isEditable = isRangeHidden();
      if (document.getElementById('txt_location') != null)
    	  {
    	  tx_location = document.getElementById('txt_location');
          locStr = tx_location.value.split(';');
    	  if (locStr.length > 1)
    		  {
    		  loc.lat = locStr[0];
    		  loc.lng = locStr[1];
    		  }
    	  }
        if (loc.lat == null)
        	{
        	if(google.loader.ClientLocation) {
       	        loc.lat = google.loader.ClientLocation.latitude;
       	        loc.lng = google.loader.ClientLocation.longitude;
       	  	}
        	else{
        		loc.lat = 41.001;
        		loc.lng = 29.001;
    			}
        	}
        var pos = new google.maps.LatLng(loc.lat, loc.lng);
        var mapOptions = {
          zoom: getMapZoomLevel(getRadius()),
          center: pos,
          mapTypeId: google.maps.MapTypeId.ROADMAP 
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
        addMarker(pos);
        if (isEditable)
        {
        	google.maps.event.addListener(map, 'click', function(event) {
        	addMarker(event.latLng);
        });
        	getGeolocation();
        }
        
      }

      // Add a marker to the map and push to the array.
      function addMarker(location) {
      if (markers.length > 0)
    		  {
    		  deleteOverlays();
    		  }
	  marker = new google.maps.Marker({
      map:map,
      draggable:isEditable,
      animation: google.maps.Animation.DROP,
      position: location
    });
	  map.setCenter(location);
	  google.maps.event.addListener(marker, 'click', toggleBounce);
      google.maps.event.addListener(marker, "dragend", function() {
      	var location = marker.position;
      	addMarker(location);
          });        

	    markers.push(marker);
        drawCircle(getRadius());
        setLocation(location);
      }

      // Sets the map on all markers in the array.
      function setAllMap(map) {
        for (var i = 0; i < markers.length; i++) {
          markers[i].setMap(map);
        }
      }

      // Removes the overlays from the map, but keeps them in the array.
      function clearOverlays() {
    	  setAllMap(null);
      }

      // Shows any overlays currently in the array.
      function showOverlays() {
        setAllMap(map);
     }

      // Deletes all markers in the array by removing references to them.
      function deleteOverlays() {
        clearOverlays();
        markers = [];
        deleteCircles();

      }
      function deleteCircles(){
    	  circle = circles.pop();
    	  circle.setMap(null);
          circles = [];
      }
      function toggleBounce() {

    	    if (marker.getAnimation() != null) {
    	      marker.setAnimation(null);
    	    } else {
    	      marker.setAnimation(google.maps.Animation.BOUNCE);
    	    }
    	  }
      function drawCircle(rad){
    	  if (rad < 1)
    		  {
    		  rad = 1;
    			if (document.getElementById('txt_range') != null)
    	    	{
    	    		document.getElementById('txt_range').value = 1;
    	    	}
    		  }
    	  var circle = new google.maps.Circle({
    		  map: map,
    		  radius: rad * 1000.0,    
    		  fillColor: '#AA0000'
    		});
    	  circle.bindTo('center', marker, 'position');
    		circles.push(circle);
      }
      
//      google.maps.event.addDomListener(window, 'load', initialize);
      
    function getRadius(){
	    var radius = 0;
		if (document.getElementById('txt_range') != null)
	    	{
	    		radius = document.getElementById('txt_range').value;
	    	}
		return radius;
    }

    function setLocation(location){
      if (document.getElementById('txt_location') != null)
  	  {
  	  tx_location = document.getElementById('txt_location');
      tx_location.value = marker.position.lat() + ';' + marker.position.lng(); 
  	  }
      else
    	  {
    	  tx_location.value = loc.lat + ';' + loc.lng; 
    	  }
    } 
    
    
	  function isRangeHidden(){
	  var isRanHidden = false;	  
	  //test txt_range.type
	  if(document.getElementById('rangeSlider') != null)
		  {
	  		isRanHidden = true;
		  }
	  return isRanHidden;
	  }
    
	  function getGeolocation()
	  {
	  if(navigator.geolocation) {
	    navigator.geolocation.getCurrentPosition(function(position) {
	      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
	      addMarker(initialLocation);
	    }, function() {
	    });
	  }
	  else 
	  {
	  }
}
